﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Ue.Infrastructure;
using Ue.Infrastructure.DomainBase;
using Ue.Infrastructure.RepositoryFramework.Repository;
using Ue.Qcdz.Domain.Resources;

namespace Ue.Qcdz.Repositories.Resources
{
    public class ResourceRepository : SqlServerRepository<Resource>,IResourceRepository
    {
        public ResourceRepository()
            : this(null) { }

        public ResourceRepository(IUnitOfWork uniteOfWork)
            : base(uniteOfWork)
        {

        }
        #region Overrides of SqlServerRepositoryBase<Resource>

        protected override string GetDataTableName()
        {
            return "Resource";
        }
        protected override string GetKeyFieldName()
        {
            return ResourceFactory.FieldNames.EntityKey;
        }
        protected override string[] GetBaseInsertFields()
        {
            return new string[]
                       {
                           ResourceFactory.FieldNames.EntityKey,
                           ResourceFactory.FieldNames.Num,
                           ResourceFactory.FieldNames.Name
                       };
        }

        protected override string[] GetBaseUpdateFields()
        {
            return new string[]
                       {
                            ResourceFactory.FieldNames.Num,
                            ResourceFactory.FieldNames.Name
                       };
        }
        protected override string GetOrderBy()
        {
            return null;
        }
        protected override IDataParameter[] BuildParameters(Resource entity)
        {
            return new IDataParameter[]
                       {
                           RepositoryDriver.BuildParameter(ResourceFactory.FieldNames.EntityKey,entity.Key),
                           RepositoryDriver.BuildParameter(ResourceFactory.FieldNames.Num,entity.Num),
                           RepositoryDriver.BuildParameter(ResourceFactory.FieldNames.Name,entity.Name)
                       };
        }
        protected override void PersistDeletedItem(Resource item)
        {
            var query = "DELETE FROM RoleInResource WHERE ResourceId=@ResourceId";
            RepositoryDriver.ExecuteNonQuery(
                 this.RepositoryDriver.BuildCommand(
                    query,
                    this.RepositoryDriver.BuildParameter("ResourceId", item.Key)
                    ));
            base.PersistDeletedItem(item);
        }
        protected override IWhereClause BuildWhereClauseBy(IDataQuery dataQuery)
        {
            throw new NotImplementedException();
        }

        protected override IDictionary<string, ChildDataHandler> GetBuildEntityChildCallbacks()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
